package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JOptionPane;

import CheckInputs.CheckInputs;
import Entity.ContactCustomer;
import Entity.Customer;
import Entity.Product;
import Entity.User;
import GUI.MarketingEmployee.AddCustomerPnl;
//import GUI.ManagerEmployee.ManagerEmployeeGUI;
//import GUI.ManagerEmployee.NewSalePnl;
import GUI.MarketingEmployee.MarketingEmployeeGUI;
import Message.Message;
import S2.client.S2Client;
import GUI.MarketingEmployee.*;


public class MarketingEmpControler implements java.awt.event.ActionListener{
	
	private static final ActionListener MarketingEmpControler = null;
	private MarketingEmployeeGUI MEGui;
	private static GUI.MarketingEmployee.AddCustomerPnl addCustomerPnl;
	private static GUI.MarketingEmployee.AddProductPnl addProductPnl;
	static  S2Client clientConection;
	
	public MarketingEmpControler(){
		if(clientConection == null)
		clientConection = MainControler.getInstance();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		//Switch by the relevant panel.
		boolean checkInput;
		
		switch (e.getActionCommand()) {
		
		//--------------------------------   Add Customer -------------------------------
		
		case "Add Customer":
			String cId;
			String cName;
			String cAge;
			String cRate;
			
			cId = MEGui.AddCustomerPnl.textFieldId.getText();
			cName = MEGui.AddCustomerPnl.textFieldName.getText();
			cAge = MEGui.AddCustomerPnl.textFieldBDate.getText();
			cRate = MEGui.AddCustomerPnl.textFieldRate.getText();
			
			if(
					!(checkInput = CheckInputs.CheckIfNumeirc(cId)) ||
					!(checkInput = CheckInputs.CheckIfNumeirc(cAge)) ||
					!(checkInput = CheckInputs.CheckIfNumeirc(cAge)) ||
					!(checkInput = CheckInputs.CheckIfNoNumeirc(cRate))
					){
						JOptionPane.showMessageDialog(null, "Invaild Input!", "InfoBox: " + "AddCustomer", JOptionPane.INFORMATION_MESSAGE);
						clearAddCusPnl();						
						break;
					}
			
			Customer customer = new Customer(cId,cName,cAge,cRate);
			Message addCustomermsg = new Message(customer,"Add Customer");
			try {
				clientConection.sendToServer(addCustomermsg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			clearAddCusPnl();
			break;
			
			
			//--------------------------------   Add Product -------------------------------		
			
		
		case "Add Product":
			String pId;
		    String pName;
		    String pDescription;
		    String pPrice;
			String pPicture;
			
			
			pId = MEGui.addProductPnl.textFieldId.getText();
			pName = MEGui.addProductPnl.textFieldName.getText();
			pDescription = MEGui.addProductPnl.textFieldDesc.getText();
			pPrice = MEGui.addProductPnl.textFieldPrice.getText();
			pPicture = MEGui.addProductPnl.textFieldPicture.getText();
			
			if(
					!(checkInput = CheckInputs.CheckIfNumeirc(pId)) ||
					!(checkInput = CheckInputs.CheckIfNoNumeirc(pName))
					){
						JOptionPane.showMessageDialog(null, "Invaild Input!", "InfoBox: " + "AddProduct", JOptionPane.INFORMATION_MESSAGE);
						clearAllProductPnl();
						break;
					}
			
			Product product = new Product(pId,pName,pDescription,pPrice,pPicture);
			
			Message addProductmsg = new Message(product,"Add Product");
			
			try {
				clientConection.sendToServer(addProductmsg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			clearAllProductPnl();
		break;
		
	//--------------------------------   Add Contact Customer -------------------------------	
		
		case "Add Contact Customer":
			String customerId;
			String employeeId;
			String cDate;
			String cTotalTime;
			String cDesc;
			String cSaleId;
			
			customerId = MEGui.addContactCustomerPnl.textFieldCusId.getText();
			employeeId = MEGui.addContactCustomerPnl.textFieldEmpId.getText();
			cDate = MEGui.addContactCustomerPnl.textFieldDate.getText();
			cTotalTime = MEGui.addContactCustomerPnl.textFieldTime.getText();
			cDesc = MEGui.addContactCustomerPnl.textFieldDesc.getText();
			cSaleId = MEGui.addContactCustomerPnl.textFieldSaleId.getText();
			
			if(
			!(checkInput = CheckInputs.Check7RangeDate(cDate)) ||
			!(checkInput = CheckInputs.CheckIfNumeirc(customerId))||
			!(checkInput = CheckInputs.CheckIfNumeirc(employeeId))||
			!(checkInput = CheckInputs.CheckIfNumeirc(cSaleId))||
			!(checkInput = CheckInputs.CheckIfNumeirc(cTotalTime))
			){
				JOptionPane.showMessageDialog(null, "Invaild Input!", "InfoBox: " + "AddContactCustomer", JOptionPane.INFORMATION_MESSAGE);
				clearAllContactPanel();
				break;
			}
			
			ContactCustomer contactCustomer = new ContactCustomer(customerId,employeeId,cDate,cTotalTime,cDesc, cSaleId);
			
			Message contactCustomermsg = new Message(contactCustomer,"Add Contact Customer");
			
			try {
				clientConection.sendToServer(contactCustomermsg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			clearAllContactPanel();
			break;
			

		default:
			System.out.println("Undefine Action!!");
			break;
		}
		
		
	}

	
	public void setView(MarketingEmployeeGUI MEmpGui) {
		this.MEGui = MEmpGui;
	}

	public static void addCustomerReturnMsg(Message msg) {
		if((boolean)msg.getObj() == true)
			JOptionPane.showMessageDialog(null, "Added Successfully", "InfoBox: " + "AddNewCustomer", JOptionPane.INFORMATION_MESSAGE);
		else 
			JOptionPane.showMessageDialog(null, "Adding New Customer Faild!", "InfoBox: " + "AddNewCustomer", JOptionPane.INFORMATION_MESSAGE);
	}
	
	public static void addProductReturnMsg(Message msg){
		
	}

	public static void addContactCusReturnMsg(Message msg) {
		if((boolean)msg.getObj() == true)
			JOptionPane.showMessageDialog(null, "Added Successfully", "InfoBox: " + "AddContactCustomer", JOptionPane.INFORMATION_MESSAGE);
		else 
			JOptionPane.showMessageDialog(null, "Adding New Contact Faild!", "InfoBox: " + "AddContactCustomer", JOptionPane.INFORMATION_MESSAGE);
		
	}
	
	
	private void clearAddCusPnl() {
		MEGui.AddCustomerPnl.textFieldId.setText(" ");
		MEGui.AddCustomerPnl.textFieldName.setText(" ");
		MEGui.AddCustomerPnl.textFieldBDate.setText(" ");
		MEGui.AddCustomerPnl.textFieldRate.setText(" ");
		
	}

	private void clearAllProductPnl() {
		MEGui.addProductPnl.textFieldName.setText(" ");
		 MEGui.addProductPnl.textFieldId.setText(" ");
		 MEGui.addProductPnl.textFieldPrice.setText(" ");
		 MEGui.addProductPnl.textFieldDesc.setText(" ");
		 MEGui.addProductPnl.textFieldPicture.setText(" ");
		
	}

	private void clearAllContactPanel() {
		MEGui.addContactCustomerPnl.textFieldCusId.setText(" ");
		MEGui.addContactCustomerPnl.textFieldEmpId.setText(" ");
		MEGui.addContactCustomerPnl.textFieldDate.setText(" ");
		MEGui.addContactCustomerPnl.textFieldTime.setText(" ");
		 MEGui.addContactCustomerPnl.textFieldDesc.setText(" ");
		 MEGui.addContactCustomerPnl.textFieldSaleId.setText(" ");
		
	}

	
}
